package com.dmy.ui;

import com.dmy.component.Letter;
import com.dmy.component.MJPanel;
import com.dmy.config.GlobleConfig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CompletableFuture;

public class MainWindow extends JFrame {

    Letter letter;
    static MainWindow mainWindow;
    public MainWindow(){
        initComponent();
         mainWindow=this;
    }

    private void initComponent() {
        this.setVisible(true);
        this.setTitle("金山打字通");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(200,200, GlobleConfig.WINDOW_WIDTH,GlobleConfig.WINDOW_HRIGHT);
        this.setResizable(false);

        MJPanel mjPanel = new MJPanel();
        letter = new Letter(mjPanel,15);
        mjPanel.setLetter(letter);

        this.add(mjPanel);
        CompletableFuture.completedFuture(mjPanel).thenAcceptAsync(m->{
           while (true){
               try {
                   Thread.sleep(200);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               mainWindow.setTitle(m.getTextInfo());
           }
        });
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                letter.keyPressed();
                switch (e.getKeyCode()){
                    case 81:
                        letter.clearOf('Q');
                        break;
                    case 87:
                        letter.clearOf('W');
                        break;
                    case 69:
                        letter.clearOf('E');
                        break;
                    case 82:
                        letter.clearOf('R');
                        break;
                    case 84:
                        letter.clearOf('T');
                        break;
                    case 89:
                        letter.clearOf('Y');
                        break;
                    case 85:
                        letter.clearOf('U');
                        break;
                    case 73:
                        letter.clearOf('I');
                        break;
                    case 79:
                        letter.clearOf('O');
                        break;
                    case 80:
                        letter.clearOf('P');
                        break;
                    case 65:
                        letter.clearOf('A');
                        break;
                    case 83:
                        letter.clearOf('S');
                        break;
                    case 68:
                        letter.clearOf('D');
                        break;
                    case 70:
                        letter.clearOf('F');
                        break;
                    case 71:
                        letter.clearOf('G');
                        break;
                    case 72:
                        letter.clearOf('H');
                        break;
                    case 74:
                        letter.clearOf('J');
                        break;
                    case 75:
                        letter.clearOf('K');
                        break;
                    case 76:
                        letter.clearOf('L');
                        break;
                    case 90:
                        letter.clearOf('Z');
                        break;
                    case 88:
                        letter.clearOf('X');
                        break;
                    case 67:
                        letter.clearOf('C');
                        break;
                    case 86:
                        letter.clearOf('V');
                        break;
                    case 66:
                        letter.clearOf('B');
                        break;
                    case 78:
                        letter.clearOf('N');
                        break;
                    case 77:
                        letter.clearOf('M');
                        break;
                }
                mjPanel.repaint();
            }
        });
    }

    public static void main(String[] args) throws InvocationTargetException, InterruptedException {

        EventQueue.invokeAndWait(()->{
            new MainWindow();
        });
    }
}
